﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Microsoft.Practices.Unity;
namespace Core.Presenters
{
    class CustomerPresenter
    {

        private ICustomerDAO _customerDAO;
        private ICustomer _customer;
        private ICustomerView _customerView;
        private ICustomer _additionalModel;

        public CustomerPresenter(ICustomerView customerView, ICustomer customer = null,object aditional = null)
        {
            _customerDAO = ResolverContainer.Resolver.Resolve<ICustomerDAO>();
            _customer = customer;
            if (customer == null)
            {
                _customer = ResolverContainer.Resolver.Resolve<ICustomer>();
                _additionalModel = ResolverContainer.Resolver.Resolve<IPrivatePerson>();
            }
            customerView.Customer.DataSource = _customer;
            customerView.AdditionalModel.DataSource = _additionalModel;
            customerView.SaveCustomer += SaveCustomer;
            _customerView = customerView;
            _customerView.ShowDialog();
            
        }

        private void SaveCustomer(object sender, EventArgs eventArgs)
        {
            
            if (_customerDAO.SaveOrUpdateAndCommit(_customer))
            {
                if (_customer.Type == "Юридическое лицо")
                {
                    var dao = ResolverContainer.Resolver.Resolve<IPrivatePersonDAO>();
                    dao.SaveOrUpdateAndCommit(_additionalModel);
                }
                else
                {
                    var dao = ResolverContainer.Resolver.Resolve<IJuridicalPersonDAO>();
                    dao.SaveOrUpdateAndCommit(_additionalModel);
                    
                }
                _customerView.Close();
            }
        }
    }
}
